<template>
  <div class="tags-view-container">
    <router-link
      v-for="(tag, index) in $store.getters.tagsViewList"
      :key="tag.fullPath"
      class="tags-view-item"
      :class="isActive(tag) ? 'active' : ''"
      :to="{ path: tag.fullPath }"
      :style="{
        backgroundColor: isActive(tag) ? $store.getters.cssVar.menuBg : '',
        borderColor: isActive(tag) ? $store.getters.cssVar.menuBg : '',
      }"
      @contextmenu.prevent="openMenu($event,index)"
    >
      {{ tag.title }}
      <el-icon
        v-show="!isActive(tag)"
        class="el-icon-close"
        @click.prevent.stop="onCloseClick(index)"
        ><Close
      /></el-icon>
    </router-link>
    <context-menu v-show="isContext" :style="menuStyle" :index="selectIndex"/>
  </div>
</template>

<script setup>
import ContextMenu from './ContextMenu.vue'
import { useRoute } from "vue-router";
import { ref,watch } from 'vue';
import { useStore } from 'vuex';

const store =useStore()
const route = useRoute();

/**
 * 是否被选中
 */
const isActive = (tag) => {
  return tag.path === route.path;
};

/**
 * 关闭 当前tag 的点击事件
 */
//关闭当前
const onCloseClick = (index) =>{
    store.commit('app/removeTagsView',{
    type:'index',
    index:index
  })
}



/**
 * contextMenu 相关
 * 鼠标右键
 */
// 控制contxt组件显示隐藏的
const isContext = ref(false)
//指定context的样式
const menuStyle = ref({
  left:0,
  top:0
})
const selectIndex = ref(0)
//contextmenu这个方法绑定的事件这个方法具体功能百度 鼠标右键触发
const openMenu = (e,index) => {
  //获取我们鼠标右键的时候 当前点击在屏幕中的坐标 通过这个坐标去设置右键context显示的位置
  const {x,y} = e
  isContext.value = true
  selectIndex.value=index
  menuStyle.value.left =x + 'px'
  menuStyle.value.top =y + 'px'
}

//关闭contextMenu的事件
const closeMenu =()=>{
  isContext.value =false
}

watch(isContext,newVal=>{
  if(newVal){
    //给当前的body绑定一个点击事件  如果当前的isCOntext为true的话就给当前的body点击一个关闭ContextMenu菜单的事件
    document.body.addEventListener('click',closeMenu)
  } else {
    document.body.removeEventListener('click',closeMenu)
  }
})

</script>

<style lang="scss" scoped>
.tags-view-container {
  height: 34px;
  width: 100%;
  background: #fff;
  border-bottom: 1px solid #d8dce5;
  box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.12), 0 0 3px 0 rgba(0, 0, 0, 0.04);
  .tags-view-item {
    display: inline-block;
    position: relative;
    cursor: pointer;
    height: 26px;
    line-height: 26px;
    border: 1px solid #d8dce5;
    color: #495060;
    background: #fff;
    padding: 0 8px;
    font-size: 12px;
    margin-left: 5px;
    margin-top: 4px;
    &:first-of-type {
      margin-left: 15px;
    }
    &:last-of-type {
      margin-right: 15px;
    }
    &.active {
      color: #fff;
      &::before {
        content: "";
        background: #fff;
        display: inline-block;
        width: 8px;
        height: 8px;
        border-radius: 50%;
        position: relative;
        margin-right: 4px;
      }
    }
    // close 按钮
    .el-icon-close {
      width: 16px;
      height: 16px;
      line-height: 10px;
      vertical-align: -1px;
      border-radius: 50%;
      text-align: center;
      transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
      transform-origin: 100% 50%;
      &:before {
        transform: scale(0.6);
        display: inline-block;
        vertical-align: -30px;
      }
      &:hover {
        background-color: #b4bccc;
        color: #fff;
      }
    }
  }
}
</style>
